#!/usr/bin/env python
#coding=utf-8
import os  
  
class FileObj:  
    def __init__(self,item_path,item_name):  
        self.path=item_path  
        self.name=item_name  
    def __str__(self):
        n=self.path.count('\\')  
        #return "┠{} {}\n".format('━'*n,self.name)  
        #print(n)
        #print('{}<a href={}>{}</a><br>\n'.format(" "*2*n,"./"+self.path.replace(' ','%20').split('/')[-1],self.name))
        return '{}<a href={}>{}</a><br>\n'.format(" "*2*n,"./"+self.path.replace(' ','%20').split('/')[-1],self.name)
        #return '{}<a href={}>{}</a><br>\n'.format(" "*2*n,self.path.replace(' ','%20'),self.name)  
 
#确认文件夹是否为一个文件的子目录
import Levenshtein
def issubfile(path):
    name=path.split('/')[-1]

    for filename in os.listdir(os.path.dirname(os.path.abspath(path))) :
        if filename==name:
            continue
        if not os.path.isdir(filename):
            if Levenshtein.ratio(filename,name)>0.8:
                return True
    return False
     
class FileDirObj:  
    def __init__(self,item_path,content=None):  
        #print('before:',item_path)  
        self.path=item_path  
        if content is None:  
            self.content=[]  
        if os.path.isdir(self.path):  
            self.name=self.path.split('\\')[-1]  
        content=[]  
        if self.name:  
            def key_func(x):  
                try:  
                    return int(x.split('、')[0])  
                except (TypeError,ValueError) as err:  
                    return 999  
              
            #temp_list=sorted(os.listdir(self.path),key=key_func)  
              
            for item in sorted(os.listdir(self.path),key=key_func):  
                temp_path=os.path.join(self.path,item)  

                if temp_path.endswith('/index.html') \
                    or temp_path.endswith('/indexhtml.py') \
                    or temp_path.endswith('.git'):
                    print('略==>'+temp_path)
                    continue
                if os.path.isdir(temp_path) and not issubfile(temp_path):
                    #continue
                    #print(temp_path)
                    content.append(FileObj(temp_path,item))  
                else:
                    if temp_path.endswith('.html'):
                        content.append(FileObj(temp_path,item))  
              
        self.content=content  
    def __str__(self):  
        n=self.path.count('\\')  
        #output="┏{} 【{}】\n".format('━'*n,self.name)  
        output='{}<a href={}>【{}】</a><br>\n'.format(" "*2*n,self.path.replace(' ','%20'),self.name)
        output='{}<a href={}>【{}】</a><br>\n'.format(" "*2*n,'../',self.name)
        for item in self.content:  
            output+="{!s}".format(item)  
        return output  
      
class FileTJ:  
    def __init__(self,path='.'):  
        self.path=path  
        self.HTML_TEMP=('<!DOCTYPE HTML>\n'  
               '<html>\n<head>\n<meta charset="utf-8">\n<title>文件目录</title>\n</head>\n'  
               '<body>\n{!s}\n</body>\n'  
               '</html>')

    def dirlist(self,path,filte=None,allfiledir=[]):
        filelist =  os.listdir(path)  
        for filename in filelist:  
            filepath = os.path.join(path, filename)  
            if os.path.isdir(filepath) and filte(filepath):
                allfiledir.append(filepath)
                self.dirlist(filepath,filte,allfiledir)  
        return allfiledir  
        
    def alldir(self):
        dirs=self.dirlist(self.path,lambda x:x != './.git')
        dirs.append(self.path)
        return dirs

    def make_html(self):
        for pa in self.alldir():
            index = os.path.join(pa, 'index.html')
            print("创----->"+index)
            with open(index,'w') as f:
                text=self.HTML_TEMP.format(FileDirObj(pa))
                f.write(text)  
  
FileTJ().make_html()  

